<html>
<head><meta charset="utf-8"><title>Allowing more explicitly assigned generic types · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html">Allowing more explicitly assigned generic types</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="246283239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246283239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246283239">(Jul 16 2021 at 21:35)</a>:</h4>
<p>Imagine for a moment that we have a generic structure with more than one generic type, such as either of these:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">VolSeries</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">R</span><span class="p">,</span><span class="w"> </span><span class="n">W</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">S</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">GenericLcg64</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">MUL</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">ADD</span>: <span class="kt">u64</span><span class="o">&gt;</span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>When a user goes to instantiate the generics it would be very easy to not remember the correct ordering for <code>R</code> and <code>W</code>, or for <code>MUL</code> and <code>ADD</code>.</p>
<p>We're actually already familiar with this problem! It happens in structs that have many fields, and so with structs we name each field we're putting a value to which makes clear what goes where. <code>Struct { f1: true, f2: false }</code>, and so on.</p>
<p>This doesn't <em>seem</em> to be possible with generics according to any of the syntaxes for how to do this that I guessed at, but it feels like it <em>should</em> be possible to express within Rust.</p>



<a name="246284537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246284537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eholk <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246284537">(Jul 16 2021 at 21:47)</a>:</h4>
<p>I wonder if we could allow <code>where</code> in more places to do something like that.</p>



<a name="246298139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246298139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246298139">(Jul 17 2021 at 02:03)</a>:</h4>
<p>I was hoping that <code>=</code> or <code>:</code> might be possible without breaking any previous part of the syntax.</p>



<a name="246630916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246630916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246630916">(Jul 20 2021 at 18:32)</a>:</h4>
<p>Could always change that second one to take a struct with <code>mul</code> and <code>add</code> fields.</p>
<p>But in general I think this is the same problem as named function arguments, with mostly the same questions (like not making names part of the public API implicitly).</p>



<a name="246631046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246631046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246631046">(Jul 20 2021 at 18:33)</a>:</h4>
<p>don't we not yet have const generic structs?</p>



<a name="246631097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246631097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246631097">(Jul 20 2021 at 18:33)</a>:</h4>
<p>are you talking about <code>const N: Foo</code> ?</p>



<a name="246632367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246632367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246632367">(Jul 20 2021 at 18:44)</a>:</h4>
<p>yes</p>



<a name="246632430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246632430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246632430">(Jul 20 2021 at 18:45)</a>:</h4>
<p>i thought min_const_generics was only int, char, bool, and arrayed variants</p>



<a name="246634512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246634512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246634512">(Jul 20 2021 at 19:03)</a>:</h4>
<p>True, but if we're talking about language changes than we can assume we'll have it at some point.</p>



<a name="246635436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246635436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246635436">(Jul 20 2021 at 19:11)</a>:</h4>
<p>Yea <code>const N: Foo</code> isnt supported yet but it'll happen :)</p>



<a name="246696818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Allowing%20more%20explicitly%20assigned%20generic%20types/near/246696818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Allowing.20more.20explicitly.20assigned.20generic.20types.html#246696818">(Jul 21 2021 at 09:28)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> I have long wished for this, yes; the problem is particularly acute with defaults. Consider <code>HashMap</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">HashMap</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RandomState</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* fields omitted */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>What happens if/when we add a <code>A = DefaultAllocator</code>? How can I set the allocator <em>without</em> setting the <code>RandomState</code>? Do I have to do <code>HashMap&lt;K, V, _, A&gt;</code>?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>